// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// A [author] rhyme_lph
// h [email]  rhymelph@gmail.com
// K [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/page/dashboard/contact/employee_contacts_page.dart';
import 'package:finance_app/src/provider/hr_payroll/employee/select_working_day_provider.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/pop/normal_border_pop.dart';
import 'package:finance_app/src/widgets/select/normal_check_box.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';

@RouterMeta(paramName: "selectWorkingDayPage")
class SelectWorkingDayPage extends StatefulWidget {
  final num workingDaysCount;
  final List<WorkingDayData> workingDays;

  const SelectWorkingDayPage({Key key, this.workingDaysCount, this.workingDays})
      : super(key: key);

  @override
  _SelectWorkingDayPageState createState() => _SelectWorkingDayPageState();
}

class _SelectWorkingDayPageState extends State<SelectWorkingDayPage>
    with PagePresenter<SelectWorkingDayPage, SelectWorkingDayProvider> {
  @override
  void initState() {
    provider.workingDaysCount = widget.workingDaysCount ?? 5;
    if (widget.workingDays != null && widget.workingDays.length != 0) {
      widget.workingDays.forEach((e) {
        provider.workingDays.add(WorkingDayData()
          ..isHalfDay = e.isHalfDay
          ..isWorking = e.isWorking);
      });
    } else {
      provider.workingDays = provider.getDefaultWorkingDays(5);
    }

    super.initState();
  }

  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.working_days, actions: [
        NormalSaveTextButton(
          onTap: provider.onSave,
        ),
      ]);

  @override
  Widget get buildEmptyWidget => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            AppSpaces.h20,
            Expanded(child: super.buildEmptyWidget),
          ],
        ),
      );

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            AppSpaces.h25,
            NormalBorderPop<num>(
              group: [
                5,
                5.5,
                6,
              ],
              select: provider.workingDaysCount,
              buildDisplayName: numberOfWorkingDays,
              onSelect: provider.onChangeNumberOfWorkingDays,
            ),
            AppSpaces.h25,
            Expanded(
                child: listBuilder<List<WorkingDayData>>(
              selector: (_, __) => List.from(__.workingDays),
              builder: (BuildContext context, List<WorkingDayData> value,
                      Widget child) =>
                  ListView.builder(
                padding: EdgeInsets.symmetric(horizontal: getDp(35)),
                itemBuilder: _buildItems,
                itemCount: value.length,
              ),
            )),
          ],
        ),
      );

  Widget _buildItems(BuildContext context, int index) {
    return listBuilder<WorkingDayData>(
        selector: (_, __) => __.workingDays[index],
        builder: (BuildContext context, WorkingDayData item, Widget child) {
          bool isWorking = item.isWorking == 1 && item.isHalfDay == 0;
          bool isHalfday = item.isWorking == 1 && item.isHalfDay == 1;

          return Row(
            children: [
              Padding(
                padding: const EdgeInsets.only(bottom: 20.0),
                child: Row(
                  children: [
                    PressedOpacityButton(
                      onPressed: () => provider.onChangeIsWorkingDay(item),
                      child: Container(
                        padding: EdgeInsets.symmetric(vertical: 8),
                        width: 140,
                        decoration: BoxDecoration(
                          color: isWorking ? aceBlueColor : null,
                          border: Border.all(color: aceBlueColor),
                          borderRadius: BorderRadius.circular(5),
                        ),
                        child: Text(
                          dayOfWeek(index),
                          textAlign: TextAlign.center,
                          style: TextStyle(
                              color: isWorking ? Colors.white : aceBlueColor,
                              fontWeight: FontWeight.w700,
                              fontSize: 16),
                        ),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(left: 20.0),
                      child: PressedOpacityButton(
                        onPressed: () => provider.onChangeIsHalfDay(item),
                        child: NormalCheckBox(
                          title: S.current.half_day,
                          isSelect: isHalfday,
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ],
          );
        });
  }

  String numberOfWorkingDays(num days) {
    String value = '';
    if (days == 5) {
      value = "5 Days";
    } else if (days == 5.5) {
      value = "5.5 Days";
    } else if (days == 6) {
      value = "6 Days";
    }
    return value;
  }

  String dayOfWeek(int index) {
    switch (index) {
      case 0:
        return S.current.monday;
        break;
      case 1:
        return S.current.tuesday;
        break;
      case 2:
        return S.current.wednesday;
        break;
      case 3:
        return S.current.thursday;
        break;
      case 4:
        return S.current.friday;
        break;
      case 5:
        return S.current.saturday;
        break;
      case 6:
        return S.current.sunday;
        break;
      default:
        return "";
    }
  }
}
